package com.chinatsp.onlineapp.ui.weather;

import java.util.ArrayList;
import java.util.List;

import tsp.cn.http.tools.LogUtil;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chinatsp.onlineapp.R;
import com.chinatsp.onlineapp.base.BaseFragment;
import com.chinatsp.onlineapp.bean.WeatherService;
import com.chinatsp.onlineapp.bean.WeatherService.RespDataBean;
import com.chinatsp.onlineapp.bean.WeatherService.RespDataBean.CurrentConditionsBean;
import com.chinatsp.onlineapp.bean.WeatherService.RespDataBean.ForecastConditionsBean;
import com.chinatsp.onlineapp.presenter.WeatherServiceDao;
import com.chinatsp.onlineapp.utils.WeatherType;

public class WeatherFragment extends BaseFragment {

	Context mContext;
	private List list_weather;
	private final int WeatherKey = 105;
	LayoutInflater mInflater;
	private ImageView iv_current_weather;
	private TextView tv_current_weather;
	private TextView tv_current_tempetature;
	private TextView tv_cityName;
	private LinearLayout ll_position;
	private TextView tv_current_data;
	private TextView tv_wind_direction;
	private TextView tv_wind_force;
	private TextView tv_airpollution_index;
	private TextView tv_ultravioletray_index;
	private TextView tv_washcar_index;
	private TextView tv_dress_index;
	private View divideLine;
	
	private List<RelativeLayout> list_rl;
	private RelativeLayout rl1,rl2,rl3,rl4,rl5;
	private TextView tv_day_date1,tv_day_date2,tv_day_date3,tv_day_date4,tv_day_date5;
	private TextView tv_day_weather1,tv_day_weather2,tv_day_weather3,tv_day_weather4,tv_day_weather5;
	private ImageView iv_day_icon1,iv_day_icon2,iv_day_icon3,iv_day_icon4,iv_day_icon5;
	private TextView tv_day_temperature1,tv_day_temperature2,tv_day_temperature3,tv_day_temperature4,tv_day_temperature5;
	
	// net
	
	WeatherServiceDao weatherServiceDao;
	
	// bean
	 RespDataBean resp_data;
	  List<ForecastConditionsBean> list_forecastConditions;
	  CurrentConditionsBean conditions;
	
	
	@Override
	public void onRequestSuccess(int requestCode) {
		super.onRequestSuccess(requestCode);
		
		
		list_forecastConditions = new ArrayList<ForecastConditionsBean>();
		conditions = new CurrentConditionsBean();
		resp_data = weatherServiceDao.getmWeatherService().getResp_data();
		
		list_forecastConditions =resp_data.getForecast_conditions();
		conditions = resp_data.getCurrent_conditions();	
		
		for (int i = 0; i <list_forecastConditions.size(); i++) {
			LogUtil.d(list_forecastConditions.get(i).getHigh_temperature()+"");
		}
		
		
//		iv_current_weather
		tv_current_weather.setText(conditions.getWeather_condition());
		tv_current_tempetature.setText(conditions.getCurrent_temperature());
		


		tv_cityName.setText(resp_data.getCity_name());
		tv_current_data.setText(resp_data.getRelease_time());
		
		
		tv_wind_direction.setText(getResources().getString(R.string.weather_t1)+conditions.getWind_direction());
		tv_wind_force.setText(getResources().getString(R.string.weather_t2)+conditions.getWind_force());
		tv_airpollution_index.setText(getResources().getString(R.string.weather_t3)+conditions.getAirpollution_index());
		tv_ultravioletray_index.setText(getResources().getString(R.string.weather_t4)+conditions.getUltravioletray_index());
		tv_washcar_index.setText(getResources().getString(R.string.weather_t5)+conditions.getWashcar_index());
		tv_dress_index.setText(getResources().getString(R.string.weather_t6)+conditions.getDress_index());
		
		class DayHolder{
			String hweek;
			String hweather;
			String hicon;
			String htemperature;
		}
		
		
		tv_day_date1.setText(list_forecastConditions.get(0).getWeather_week());
		tv_day_date2.setText(list_forecastConditions.get(1).getWeather_week());
		tv_day_date3.setText(list_forecastConditions.get(2).getWeather_week());
		tv_day_date4.setText(list_forecastConditions.get(3).getWeather_week());
		tv_day_date5.setText(list_forecastConditions.get(4).getWeather_week());
		
		tv_day_weather1.setText(list_forecastConditions.get(0).getWeather_condition());
		tv_day_weather2.setText(list_forecastConditions.get(1).getWeather_condition());
		tv_day_weather3.setText(list_forecastConditions.get(2).getWeather_condition());
		tv_day_weather4.setText(list_forecastConditions.get(3).getWeather_condition());
		tv_day_weather5.setText(list_forecastConditions.get(4).getWeather_condition());		
		
		tv_day_temperature1.setText(list_forecastConditions.get(0).getLow_temperature()+"/"+list_forecastConditions.get(0).getHigh_temperature());
		tv_day_temperature2.setText(list_forecastConditions.get(1).getLow_temperature()+"/"+list_forecastConditions.get(1).getHigh_temperature());
		tv_day_temperature3.setText(list_forecastConditions.get(2).getLow_temperature()+"/"+list_forecastConditions.get(2).getHigh_temperature());
		tv_day_temperature4.setText(list_forecastConditions.get(3).getLow_temperature()+"/"+list_forecastConditions.get(3).getHigh_temperature());
		tv_day_temperature5.setText(list_forecastConditions.get(4).getLow_temperature()+"/"+list_forecastConditions.get(4).getHigh_temperature());

		WeatherType.showImg(list_forecastConditions.get(0).getWeather_condition(),iv_day_icon1);
		WeatherType.showImg(list_forecastConditions.get(1).getWeather_condition(),iv_day_icon2);
		WeatherType.showImg(list_forecastConditions.get(2).getWeather_condition(),iv_day_icon3);
		WeatherType.showImg(list_forecastConditions.get(3).getWeather_condition(),iv_day_icon4);
		WeatherType.showImg(list_forecastConditions.get(4).getWeather_condition(),iv_day_icon5);
		
//		for (int i = 0; i <5; i++) {
//			list_forecastConditions.get(i).getWeather_type();
//			
//			LogUtil.e(list_forecastConditions.get(i).getWeather_condition()+"");
//		}
		
	}
	

	
	public WeatherFragment() {
		mContext = getActivity();

	}
    /**
     * 第一次fragment可见（进行初始化工作）
     */
    public void onFirstUserVisible() {
    	//获取天气数据
		weatherServiceDao = new WeatherServiceDao(getActivity(), this);
    	weatherServiceDao.getWeatherService("北京");
    	
    }
    
    @Override
    public void onRequestFailed(String errorNo, String errorMessage) {
    	super.onRequestFailed(errorNo, errorMessage);

    }
    

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View root = LayoutInflater.from(getActivity()).inflate(
				R.layout.fragment_weather, container, false);
		// current weather
		iv_current_weather = (ImageView) root.findViewById(R.id.iv_now_weather);
		tv_current_weather = (TextView) root.findViewById(R.id.tv_now_weather);
		tv_current_tempetature = (TextView) root.findViewById(R.id.tv_now_temperature);
		ll_position = (LinearLayout) root.findViewById(R.id.ll_position);
		tv_cityName = (TextView) root.findViewById(R.id.tv_position);
		tv_current_data = (TextView) root.findViewById(R.id.tv_current_data);
		tv_wind_direction = (TextView) root.findViewById(R.id.tv_wind_direction);
		tv_wind_force = (TextView) root.findViewById(R.id.tv_wind_force);
		tv_airpollution_index = (TextView) root.findViewById(R.id.tv_airpollution_index);
		tv_ultravioletray_index = (TextView) root.findViewById(R.id.tv_ultravioletray_index);
		tv_washcar_index = (TextView) root.findViewById(R.id.tv_washcar_index);
		tv_dress_index = (TextView) root.findViewById(R.id.tv_dress_index);
		// forecast_conditions
		rl1 = (RelativeLayout) root.findViewById(R.id.rl_item1);
		rl2 = (RelativeLayout) root.findViewById(R.id.rl_item2);
		rl3 = (RelativeLayout) root.findViewById(R.id.rl_item3);
		rl4 = (RelativeLayout) root.findViewById(R.id.rl_item4);
		rl5 = (RelativeLayout) root.findViewById(R.id.rl_item5);
		list_rl = new ArrayList<RelativeLayout>();
		list_rl.add(rl1); list_rl.add(rl2); list_rl.add(rl3);  list_rl.add(rl4); list_rl.add(rl5);    
		tv_day_date1 = (TextView) root.findViewById(R.id.tv_day_date1);
		tv_day_date2 = (TextView) root.findViewById(R.id.tv_day_date2);
		tv_day_date3 = (TextView) root.findViewById(R.id.tv_day_date3);
		tv_day_date4 = (TextView) root.findViewById(R.id.tv_day_date4);
		tv_day_date5 = (TextView) root.findViewById(R.id.tv_day_date5);
		
		tv_day_weather1 = (TextView) root.findViewById(R.id.tv_day_weather1);
		tv_day_weather2 = (TextView) root.findViewById(R.id.tv_day_weather2);
		tv_day_weather3 = (TextView) root.findViewById(R.id.tv_day_weather3);
		tv_day_weather4 = (TextView) root.findViewById(R.id.tv_day_weather4);
		tv_day_weather5 = (TextView) root.findViewById(R.id.tv_day_weather5);
		
		iv_day_icon1 = (ImageView) root.findViewById(R.id.iv_day_icon1);
		iv_day_icon2 = (ImageView) root.findViewById(R.id.iv_day_icon2);
		iv_day_icon3 = (ImageView) root.findViewById(R.id.iv_day_icon3);
		iv_day_icon4 = (ImageView) root.findViewById(R.id.iv_day_icon4);
		iv_day_icon5 = (ImageView) root.findViewById(R.id.iv_day_icon5);
		
		tv_day_temperature1 = (TextView) root.findViewById(R.id.tv_day_temperature1);
		tv_day_temperature2 = (TextView) root.findViewById(R.id.tv_day_temperature2);
		tv_day_temperature3 = (TextView) root.findViewById(R.id.tv_day_temperature3);
		tv_day_temperature4 = (TextView) root.findViewById(R.id.tv_day_temperature4);
		tv_day_temperature5 = (TextView) root.findViewById(R.id.tv_day_temperature5);
		
		ll_position.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				Intent intent = new Intent();
				intent.setClass(getActivity(), WeatherSelectActivity.class);
				startActivity(intent);
				
			}
		});
		
		
		
//		divideLine = root.findViewById(R.id.v_line);
//		
//		
//		LayoutParams layoutParams = tv_current_data.getLayoutParams();
//		int width = layoutParams.width;
//		LayoutParams lp = divideLine.getLayoutParams();
//		LogUtil.e("divide width:"+ lp.width);
//		LogUtil.e("tv width:"+ layoutParams.width);
//		lp.width = width;
//		divideLine.setLayoutParams(lp);



		return root;
	}
	
	
	
	
	
}
